/**
 *  *2010-7-20 下午04:28:11  add 
 */
package com.base.myproject.client.HR.Salary;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.HR.bcode.HrBcodeSelectButton;
import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**  工资策略
 * @author Administrator 2010-7-20 javazgw@gmail.com
 */
public class Salary_Ploy extends BaseJDBCPanel {

	BaseStore<BusiObject> store = null;
	TextArea mark;
	String name1 = "";
	String name2 = "";
	String name3 = "";
	String name4 = "";
	HrBcodeSelectButton hrbsb ;
	HrBcodeSelectButton hrbsb_lw ;
	ColumnConfig systembcodecolumn ;
	ColumnConfig systembcodecolumn_lw;
	
	public Salary_Ploy() {
		String sql = "select * from hr_Salary_Item where ifSalary = 1 ";
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT.create(com.base.myproject.client.tools.GreetingService.class);
		   greetingService2.getDataSet(sql, new AsyncCallback<DataSet>(){
			@Override
			public void onFailure(Throwable caught) {	
			}
			@Override
			public void onSuccess(DataSet result) {
				name1 = result.getValue(0, "desc_cn");
				name2 = result.getValue(1, "desc_cn");
				name3 = result.getValue(2, "desc_cn");
				name4 = result.getValue(3, "desc_cn");
				init();
			}
		});
	}

	public void init() {
		initrenderer();
		initWedgitx();
		registerMainStore(mainstore);
		registerStore(mainstore);
	}

	private void initWedgitx() {
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.setSpacing(2);
		hpanel.add(createMGrid());
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.setSpacing(1);
		vpanel.add(createMForm());
		vpanel.add(createGrid());

		initSelectGrid();
		TabPanel panel = new TabPanel();
		panel.setPlain(true);
		panel.setSize(810, 160);
		
		TabItem zz_tab = new TabItem("组织");
		zz_tab.add(createBcodeGrid());
		hrbsb = new HrBcodeSelectButton(){
			public void doposted(List<TreeNodeModelData> modeldatalist) {
				for(TreeNodeModelData tnmd : modeldatalist){
					HashMap hm = new HashMap();
					hm.put("systembcode", tnmd.get("systembcode"));
					hm.put("sheetcode", "Salary_Ploy");//这里是针对每个类用到组织条件的时候 添加进去类名  用于区分
					this.getTarget_store().add(hm);
				}
			}
		};
		hrbsb.setCheckNodes(CheckNodes.BOTH);
		hrbsb.setNeesreselect(true);
		hrbsb.setTarget_store(bcode_store);
		zz_tab.setAutoHeight(true);
		panel.add(zz_tab);
		
		TabItem zy_tab = new TabItem("职级");
		zy_tab.addStyleName("pad-text");
		zy_tab.add(zy_Grid);
		panel.add(zy_tab);
		
		TabItem ji_neng = new TabItem("雇佣组别");// hr_jobgroup
		ji_neng.addStyleName("pad-text");
		ji_neng.add(jobgroup_Grid);
		panel.add(ji_neng);

		TabItem z_j = new TabItem("雇佣性质"); // hr_jobproperty
		z_j.addStyleName("pad-text");
		z_j.add(jobproperty_Grid);
		panel.add(z_j);

		TabItem lb_tab = new TabItem("雇佣类别"); // hr_jobtype
		lb_tab.addStyleName("pad-text");
		lb_tab.add(jobtype_Grid);
		panel.add(lb_tab);

		TabItem zyzk_tab = new TabItem("雇佣状况"); // hr_jobtype
		zyzk_tab.addStyleName("pad-text");
		zyzk_tab.add(zyzk_Grid);
		panel.add(zyzk_tab);
		
		TabItem zyzk_hjq = new TabItem("户籍区"); // hr_jobtype
		zyzk_hjq.addStyleName("pad-text");
		zyzk_hjq.add(hjq_Grid);
		panel.add(zyzk_hjq);
		
		TabItem zyzk_cx = new TabItem("城/乡"); // hr_jobtype
		zyzk_cx.addStyleName("pad-text");
		zyzk_cx.add(cx_Grid);
		panel.add(zyzk_cx);
		
		TabItem lw_tab = new TabItem("例外");
		lw_tab.add(createlwGrid());
		hrbsb_lw = new HrBcodeSelectButton(){
			public void doposted(List<TreeNodeModelData> modeldatalist) {
				for(TreeNodeModelData tnmd : modeldatalist){
					HashMap hm = new HashMap();
					hm.put("systembcode", tnmd.get("systembcode"));
					hm.put("sheetcode", "Salary_Ploy");
					this.getTarget_store().add(hm);
				}
			}
		};
		hrbsb_lw.setCheckNodes(CheckNodes.BOTH);
		hrbsb_lw.setNeesreselect(true);
		hrbsb_lw.setTarget_store(lw_store);
		lw_tab.setAutoHeight(true);
		lw_tab.setAutoHeight(true);
		panel.add(lw_tab);
		
		vpanel.add(panel);
		hpanel.add(vpanel);
		add(hpanel);
		this.layout();
	}
	
	
	
	//************************************************************************组织
	
	BaseStore<BusiObject> bcode_store = null;
	BaseStore<BusiObject> getBcodeStore() {
		if (bcode_store == null) {
			createBcodeStore();
		}
		return bcode_store;
	}
	BaseStore<BusiObject> createBcodeStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("systembcode");
		type.getField("systembcode").setType(String.class);
		type.addField("upcode");
		type.getField("upcode").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_range_bcode");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_range_bcode where sheetcode='Salary_Ploy' and upcode ='{icode}'");
	    info.setParent(mainstore);// 关联主表
		info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "upcode" });//
		bcode_store = new BaseStore<BusiObject>(type, info);
		return bcode_store;
	}
	BaseGridPanel bcodegrid = null;
	BaseGridPanel createBcodeGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		systembcodecolumn = new ColumnConfig("systembcode", "部门人员", 80);
		columns.add(systembcodecolumn);
		ColumnModel cm = new ColumnModel(columns);
		bcodegrid = new BaseGridPanel(getBcodeStore(), cm, true, true)
		{
			protected void addnewrecord() {
				addrecord();
			}
		};
		cbs.setSelectionMode(SelectionMode.SINGLE);
		bcodegrid.getBg().setSelectionModel(cbs);
		bcodegrid.getBg().setHeight(110);
		bcodegrid.getBg().setBorders(false);
		return bcodegrid;
	}	
	void addrecord() {
		hrbsb.showEditor();
	}	
	
	//例外的grid
	
	BaseStore<BusiObject> lw_store = null;
	BaseStore<BusiObject> getlwStore() {
		if (lw_store == null) {
			createlwStore();
		}
		return lw_store;
	}
	BaseStore<BusiObject> createlwStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("systembcode");
		type.getField("systembcode").setType(String.class);
		type.addField("upcode");
		type.getField("upcode").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_range_lw");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_range_lw where sheetcode='Salary_Ploy' and upcode ='{icode}'");
		info.setParent(mainstore);// 关联主表
		info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "upcode" });//
		lw_store = new BaseStore<BusiObject>(type, info);
		return lw_store;
	}
	
	BaseGridPanel lwgrid = null;
	BaseGridPanel createlwGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		systembcodecolumn_lw = new ColumnConfig("systembcode", "部门人员", 80);
		columns.add(systembcodecolumn_lw);
		ColumnModel cm = new ColumnModel(columns);
		lwgrid = new BaseGridPanel(getlwStore(), cm, true, true)
		{
			protected void addnewrecord() {
				addlwrecord();
			}
		};
		cbs.setSelectionMode(SelectionMode.SINGLE);
		lwgrid.getBg().setSelectionModel(cbs);
		lwgrid.getBg().setHeight(110);
		lwgrid.getBg().setBorders(false);
		return lwgrid;
	}	

	public void addlwrecord() {
		hrbsb_lw.showEditor();
	}	
	
	
	public void initrenderer(){
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet("select systembcode,bname from systembcode_view ",new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
				Info.display("", "失败");
			}

			@Override
			public void onSuccess(final DataSet result) {
				Info.display("", "成功");
				 GridCellRenderer gcr = new GridCellRenderer(){

					@Override
					public Object render(ModelData model, String property,
							ColumnData config, int rowIndex, int colIndex,
							ListStore store, Grid grid) {
						// TODO Auto-generated method stub
						for(int i=0;i<result.getRowCount();i++){
							
							if(result.getValue(i, "systembcode").equals(model.get(property))){
								return	result.getValue(i, "bname");
							}
						}
						return model.get(property);
					}
				};
				systembcodecolumn.setRenderer(gcr);
				bcodegrid.getBg().getView().refresh(false);
				systembcodecolumn_lw.setRenderer(gcr);
				lwgrid.getBg().getView().refresh(false);
				
			}

		});
	}
	
	SelectGrid zy_Grid;
	SelectGrid jobgroup_Grid;
	SelectGrid jobproperty_Grid;
	SelectGrid jobtype_Grid;
	SelectGrid zyzk_Grid;
	SelectGrid hjq_Grid;
	SelectGrid  cx_Grid;
	
	public void initSelectGrid() {
		//职级
		zy_Grid = new SelectGrid(
				mainstore,
				"hr_range_zy_c",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'zy' ) as a left join  (select code,isset,upcode from hr_range_zy_c where upcode = '{icode}' and sheetcode='Salary_ploy') as b on a.code = b.code",
				"icode","Salary_Ploy");	
		// 雇佣组别
		jobgroup_Grid = new SelectGrid(
				mainstore,
				"hr_range_jobgroup",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_jobgroup' ) as a left join  (select code,isset,upcode from hr_range_jobgroup where upcode = '{icode}' and sheetcode='Salary_ploy') as b on a.code = b.code",
				"icode","Salary_Ploy");
		// 雇佣性质
		jobproperty_Grid = new SelectGrid(
				mainstore,
				"hr_range_jobproperty",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_jobproperty' ) as a left join  (select code,isset,upcode from hr_range_jobproperty where upcode = '{icode}' and sheetcode='Salary_ploy') as b on a.code = b.code",
				"icode","Salary_Ploy");
		// 雇佣类别
		jobtype_Grid = new SelectGrid(
				mainstore,
				"hr_range_jobtype",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_jobtype' ) as a left join  (select code,isset,upcode from hr_range_jobtype where upcode = '{icode}' and sheetcode='Salary_ploy') as b on a.code = b.code",
				"icode","Salary_Ploy");
		// 雇佣状况
		zyzk_Grid = new SelectGrid(
				mainstore,
				"hr_range_zyzk",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'zyzk' ) as a left join  (select code,isset,upcode from hr_range_zyzk where upcode = '{icode}' and sheetcode='Salary_ploy') as b on a.code = b.code",
				"icode","Salary_Ploy");
		
		//户籍区
		hjq_Grid = new SelectGrid(
				mainstore,
				"hr_range_hjq",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_a' ) as a left join  (select code,isset,upcode from hr_range_hjq where upcode = '{icode}' and sheetcode='Salary_ploy') as b on a.code = b.code",
				"icode","Salary_Ploy");
		//城乡
		cx_Grid = new SelectGrid(
				mainstore,
				"hr_range_cx",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_cz' ) as a left join  (select code,isset,upcode from hr_range_cx where upcode = '{icode}' and sheetcode='Salary_ploy') as b on a.code = b.code",
				"icode","Salary_Ploy");
		
		zy_Grid.grid.setHeight(125);
		jobgroup_Grid.grid.setHeight(125);
		jobproperty_Grid.grid.setHeight(125);
		jobtype_Grid.grid.setHeight(125);
		zyzk_Grid.grid.setHeight(125);
		hjq_Grid.grid.setHeight(125);
		cx_Grid.grid.setHeight(125);
		zy_Grid.setBorders(false);
		jobgroup_Grid.setBorders(false);
		jobproperty_Grid.setBorders(false);
		jobtype_Grid.setBorders(false);
		zyzk_Grid.setBorders(false);
		hjq_Grid.setBorders(false);
		cx_Grid.setBorders(false);

	}
	
	/***********************************左边主面板**************************************/
	
	BaseStore<BusiObject> mainstore = null;

	BaseStore<BusiObject> getMStore() {
		if (mainstore == null) {
			createMStore();
		}
		return mainstore;
	}

	BaseStore<BusiObject> createMStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("codename");
		type.getField("codename").setType(String.class);
		type.addField("mark");
		type.getField("mark").setType(String.class);
		type.addField("ploystartdate");
		type.getField("ploystartdate").setType(Date.class);
		type.addField("ployenddate");
		type.getField("ployenddate").setType(Date.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_salary_ploy_m");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_salary_ploy_m");
		mainstore = new BaseStore<BusiObject>(type, info){};
		
		return mainstore;
	}

	BaseGridPanel maingrid = null;

	BaseGridPanel createMGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C2 = new ColumnConfig("codename", "名称：", 100);
		columns.add(C2);
		ColumnModel cm = new ColumnModel(columns);
		maingrid = new BaseGridPanel(getMStore(), cm, true, true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		maingrid.getBg().setSelectionModel(cbs);
		maingrid.getBg().setHeight(453);
		maingrid.getBg().setWidth(175);
		return maingrid;
	}

	/***********************************查询部分**************************************/
	
	BaseFormPanel createMForm() {
		BaseFormPanel mainform = new BaseFormPanel(getMStore());
		TableLayout tl = new TableLayout(6);

		TableData td = new TableData();
		TableData td2 = new TableData();
		td2.setColspan(5);
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		
		td2.setRowspan(1);
		td2.setHorizontalAlign(HorizontalAlignment.LEFT);
		td2.setVerticalAlign(VerticalAlignment.TOP);
		td2.setPadding(2);
		
		mainform.setLayout(tl);
		mainform.setHeaderVisible(false);

		mainform.add(new Label("名称:"), td);
		TextField<String> C_num2 = new TextField<String>();
		C_num2.setName("codename");
		C_num2.setAllowBlank(false);
		mainform.add(C_num2, td);
		
		mainform.add(new Label("策略有效期从:"), td);
		DateField C_num3 = new DateField();
		C_num3.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_num3.setName("ploystartdate");
		C_num3.setAllowBlank(false);
		mainform.add(C_num3, td);
		
		
		mainform.add(new Label("策略有效期到:"), td);
		DateField C_num4 = new DateField();
		C_num4.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_num4.setAllowBlank(false);
		C_num4.setName("ployenddate");
		mainform.add(C_num4, td);
		
		mainform.setWidth(810);
		mainform.setHeight(60);
		mainform.inited();

		return mainform;
	};

	/***********************************中间工资策略**************************************/
	
	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("codename");
		type.getField("codename").setType(String.class);
		type.addField("salaryfrom");
		type.getField("salaryfrom").setType(Double.class);
		type.addField("salaryto");
		type.getField("salaryto").setType(Double.class);
		type.addField("baserate");
		type.getField("baserate").setType(Double.class);
		type.addField("positionrate");
		type.getField("positionrate").setType(Double.class);
		type.addField("defined_salary3");
		type.getField("defined_salary3").setType(Double.class);
		type.addField("defined_salary4");
		type.getField("defined_salary4").setType(Double.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_salary_ploy");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_salary_ploy where codename='{codename}' order by salaryto asc");
		info.setParent(mainstore);// 关联主表
		info.setLinkparentcolumns(new String[] { "codename" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "codename" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		
		return store;
	}

	BaseGrid<BusiObject> grid = null;
	NumberField C_num3;
	NumberField C_num4;
	NumberField C_num5;
	NumberField C_num6;
	NumberField C_num7;
	NumberField C_num8;
	NumberFormat nf2 = NumberFormat.getFormat("#,##0%");
	
	 GridCellRenderer<BusiObject> change = new GridCellRenderer<BusiObject>() {  
	      public String render(BusiObject model, String property, ColumnData config, int rowIndex, int colIndex,  
	          ListStore<BusiObject> store, Grid<BusiObject> grid) { 
	    	  double val = 0;
	    	if (model.get(property) != null) {
	    		val = (Double) model.get(property);
	    	} 
	        String style ="blue";  
	        if(val<0){
	        	style ="red";  
	        	return "<span style='color:" + style + "'>" + val + "</span>";
	        }else if(val>0 && val<=1){
	        	style ="black";  
	 	        return "<span style='color:" + style + "'>" + nf2.format(val) + "</span>";
	        }
	        return "<span style='color:" + style + "'>" + val + "</span>";
	      }  
	    };  
	public BaseGrid<BusiObject> createGrid() {
		
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C2 = new ColumnConfig("codename", "名称", 80);
		columns.add(C2);
		
		ColumnConfig C3 = new ColumnConfig("salaryfrom", "工资（从）", 80);
		C_num3 = new NumberField();
		C_num3.setPropertyEditorType(Double.class);
		C_num3.setName("salaryfrom");
		C3.setEditor(new CellEditor(C_num3));
		columns.add(C3);
		
		ColumnConfig C4 = new ColumnConfig("salaryto", "工资（到）", 80);
		C_num4 = new NumberField();
		C_num4.setPropertyEditorType(Double.class);
		C_num4.setName("salaryto");
		C4.setEditor(new CellEditor(C_num4));
		columns.add(C4);
		
		ColumnConfig C5 = new ColumnConfig("baserate", name1, 80);
		C_num5 = new NumberField();
		C_num5.setPropertyEditorType(Double.class);
		C_num5.setName("baserate");
		C5.setRenderer(change);
		C5.setEditor(new CellEditor(C_num5));
		C5.setNumberFormat(nf2);
		columns.add(C5);
		
		ColumnConfig C6 = new ColumnConfig("positionrate", name2, 80);
		C_num6 = new NumberField();
		NumberFormat nf = NumberFormat.getFormat("#,##0%");
		C6.setNumberFormat(nf);
		C_num6.setPropertyEditorType(Double.class);
		C_num6.setName("positionrate");
		C6.setRenderer(change);
		C6.setEditor(new CellEditor(C_num6));
		columns.add(C6);
		
		ColumnConfig C7 = new ColumnConfig("defined_salary3", name3, 80);
		C_num7 = new NumberField();
		NumberFormat nf3 = NumberFormat.getFormat("#,##0%");
		C7.setNumberFormat(nf3);
		C_num7.setPropertyEditorType(Double.class);
		C_num7.setName("defined_salary3");
		C7.setRenderer(change);
		C7.setEditor(new CellEditor(C_num7));
		columns.add(C7);
		
		ColumnConfig C8 = new ColumnConfig("defined_salary4", name4, 80);
		C_num8 = new NumberField();
		NumberFormat nf4 = NumberFormat.getFormat("#,##0%");
		C8.setNumberFormat(nf4);
		C_num8.setPropertyEditorType(Double.class);
		C_num8.setName("defined_salary4");
		C8.setRenderer(change);
		C8.setEditor(new CellEditor(C_num8));
		columns.add(C8);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm, true, true);
		grid.addListener(Events.AfterEdit, getListener());
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setHeight(250);
		grid.setWidth(810);
		C_num3.setFireChangeEventOnSetValue(true);
		return grid;
	}
	
	int sync = 0;
	int index = -1;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(" 按钮变量")) {

							}
						} else if (be.getType() == Events.AfterEdit) {
							GridEvent<BusiObject> ge = (GridEvent)be;
							index = ge.getRowIndex();
							if( ge.getProperty().equals("baserate")){//可同时监听多个字段ge.getProperty().equals("amount") ||
								checkSalary(index);
							}else if(ge.getProperty().equals("positionrate")){
								checkSalary(index);
							}else if(ge.getProperty().equals("defined_salary3")){
								checkSalary(index);
							}else if(ge.getProperty().equals("defined_salary4")){
								checkSalary(index);
							}

						} else if (be.getType() == Events.KeyUp) {
							
						}
					}
					}finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
	
	public boolean checkSalary(int index){
		Double value1 = (Double)store.getValue(index, "baserate");
		Double value2 = (Double)store.getValue(index, "positionrate");
		Double value3 = (Double)store.getValue(index, "defined_salary3");
		Double value4 = (Double)store.getValue(index, "defined_salary4");
		Double value5 = (Double)store.getValue(index, "salaryto");
		
		if(value1 == -1 || value2 == -1 || value3 == -1 || value4 == -1){
			if((value1 + value2 + value3 + value4) > value5){
				MessageBox.alert("提示", "工资输入有误，不能超过" + value5, null);
				mainstore.reload();
				return false;
			}else{
				return true;
			}
			
		}else{
			if((value1 + value2 + value3 + value4) > 1.0){
				MessageBox.alert("提示", "工资比例输入有误，不能超过100%", null);
				mainstore.reload();
				return false;
			}else{
				return true;
			}
		}
	}
	
	public boolean checkSalarySave(int index){
		Double value1 = store.getValue(index, "baserate") == null? 0:(Double)store.getValue(index, "baserate");
		Double value2 = store.getValue(index, "positionrate") == null? 0:(Double)store.getValue(index, "positionrate");
		Double value3 = store.getValue(index, "defined_salary3") == null? 0:(Double)store.getValue(index, "defined_salary3");
		Double value4 = store.getValue(index, "defined_salary4") == null? 0:(Double)store.getValue(index, "defined_salary4");
		Double value5 = store.getValue(index, "salaryto") == null? 0:(Double)store.getValue(index, "salaryto");
		
		if(value1 == -1 || value2 == -1 || value3 == -1 || value4 == -1){
			if((value1 + value2 + value3 + value4) > value5){
				return false;
			}else{
				return true;
			}
			
		}else{
			if((value1 + value2 + value3 + value4) > 1.0){
				return false;
			}else{
				return true;
			}
		}
	}
	protected void save() {
		if(index == -1){
			mainstore.trySave();
		}else{
			if(checkSalarySave(index)){
				mainstore.trySave();
			}
		}
	}
}
